import { da } from 'element-plus/es/locales.mjs'
import request from '../../../utils/request'
import type { RoleResponseData, RoleData, MenuResponseData } from './type'

enum API {
  // 获取所有已有管理用户
  ALLROLE_URL = '/admin/acl/role/',
  // 添加职位
  ADDROLE_URL = '/admin/acl/role/save',
  // 修改职位名称
  UPDATEDROLE_URL = '/admin/acl/role/update',
  // 删除职位
  REMOVEROLE_URL = '/admin/acl/role/remove/',
  // 根据用户获取权限列表
  ROLEMENU_URL = '/admin/acl/permission/toAssign/',
  // 给角色分配权限
  DOASSIGN_URL = '/admin/acl/permission/doAssign?',
}

/* 获取已有职位信息 */
export const reqRoleInfo = (page: number, limit: number, roleName: string) =>
  request.get<any, RoleResponseData>(
    API.ALLROLE_URL + `${page}/${limit}/?roleName=${roleName}`,
  )

/* 添加或者修改一个职位 */
export const reqAddOrUpdateRole = (data: RoleData) => {
  if (data.id) {
    return request.put(API.UPDATEDROLE_URL, data)
  } else {
    return request.post(API.ADDROLE_URL, data)
  }
}

/* 删除职位_根据id删除 */
export const reqDeleteRole = (id: number) =>
  request.delete<any, any>(API.REMOVEROLE_URL + id)

/* 获取权限数据 */
export const reqGetRoleList = (roleId: number) =>
  request.get<any, MenuResponseData>(API.ROLEMENU_URL + roleId)

/* 分配权限 */
export const reqAssignPermission = (roleId: number, permissionId: number[]) =>
  request.post(
    API.DOASSIGN_URL + `roleId=${roleId}&permissionId=${permissionId}`,
  )
